package com.edu.atams.service.impl;

import com.edu.atams.common.dto.GradeClassDto;
import com.edu.atams.entity.Class;
import com.edu.atams.entity.ClassIdName;
import com.edu.atams.entity.ClassSelectList;
import com.edu.atams.entity.Grade;
import com.edu.atams.mapper.ClassMapper;
import com.edu.atams.service.ClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    @Autowired
    ClassMapper classMapper;

    @Override
    public Integer getAllNum() {
        List<Class> list = this.list();
        return list.size();
    }

    @Override
    public List<GradeClassDto> getClassList(Integer id) {
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        List<ClassSelectList> classSelectList = classMapper.getClasslist(id,year);
        List<String> gradeNameList = classSelectList.stream().map(ClassSelectList::getGradename).distinct().collect(Collectors.toList());

        ArrayList<GradeClassDto> gradeClassDtos = new ArrayList<>();
        for (String s : gradeNameList) {
            GradeClassDto gradeClassDto=new GradeClassDto();
            gradeClassDto.setGradename(s);
            List<ClassIdName> classIdNames=new ArrayList<>();
            for (ClassSelectList list : classSelectList) {
                if(list.getGradename().equals(s)){
                    ClassIdName classIdName=new ClassIdName();
                    classIdName.setId(list.getId());
                    classIdName.setName(list.getClassname());
                    classIdNames.add(classIdName);
                }
            }
            gradeClassDto.setClassIdNames(classIdNames);
            gradeClassDtos.add(gradeClassDto);
        }
        return gradeClassDtos;
    }
}
